package com.haiyi.activiti.engine.framework.database;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import java.util.List;

/**
 * <p> 继承自ServiceImpl,方便后续扩展一些mybatisPlus的方法 </p>
 *
 * <pre> Created: 2018/12/5 14:16 </pre>
 *
 * @author hlu
 * @version 1.0
 * @since JDK 1.7
 */
public abstract class AbstractServiceImpl<M extends HaiyiMapper<T,Q> ,
        T extends Serializable,Q extends Query<? extends Query<?,?>,T>>
        extends ServiceImpl<M,T> implements HaiyiService<T,Q>, ApplicationContextAware {

    private ApplicationContext applicationContext;

    @Override
    public List<T> query(Q query) {
        return baseMapper.query(query);
    }

    @Override
    public Long count(Q query) {
        return baseMapper.count(query);
    }

    @Override
    public Q createQuery() {
        return this.applicationContext.getBean(getQueryInterfaceType());
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    private Class<Q> getQueryInterfaceType() {
        return (Class)((ParameterizedType)this.getClass().getGenericSuperclass()).getActualTypeArguments()[2];
    }
}
